/*
 * Copyright (c) 2018. Beyondsoft Corporation.
 * All Rights Reserved.
 *
 * BEYONDSOFT CONFIDENTIALITY
 *
 * The information in this file is confidential and privileged from Beyondsoft Corporation,
 * which is intended only for use solely by Beyondsoft Corporation.
 * Disclosure, copying, distribution, or use of the contents of this file by persons other than Beyondsoft Corporation
 * is strictly prohibited and may violate applicable laws.
 */
package com.beyondsoft.smarthome.base;

import android.content.Context;
import android.content.res.Configuration;
import android.os.Build;
import android.text.TextUtils;
import cn.jpush.android.api.JPushInterface;
import com.beyondsoft.smarthome.bean.LoginBean;
import com.beyondsoft.smarthome.bean.responcebean.PermissionTabBean;
import com.beyondsoft.smarthome.utils.SharedPreferencesUtil;
import com.beyondsoft.smarthome.utils.constant.Constant;
import com.beyondsoft.smarthome.utils.logs.LogTag;
import com.tencent.bugly.crashreport.CrashReport;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

/**
 * @author xzk
 * @data 2019/5/23
 * @email xiezhengkun@beyondsoft.com
 * @remark  app初始化辅助类
 */
public class MainAppHelper {

    private static boolean isLogDebug=true;
    private static boolean isJPushDebug=true;
    private static boolean isStopBuglyDebug=false;
    private static String  buglyAppID="c892ff3a24";
    /**
     *
     * 初始化log库 支持写入日志到文件
     */
    public static void initLog(){
        LogTag.setDebug(isLogDebug);
        JPushInterface.setDebugMode(isJPushDebug);
        JPushInterface.init(MainApp.getInstance());
    }

    /**
     * 权限列表数据
     */
    private static ArrayList<PermissionTabBean.DataBean> permissionDataBeans;

    public static void initBugCrash(MainApp appContext){
        if(isStopBuglyDebug){ return;}
        // 获取当前包名
        String packageName = appContext.getPackageName();
        // 获取当前进程名

        String processName = getProcessName(android.os.Process.myPid());
        // 设置是否为上报进程
        CrashReport.UserStrategy strategy = new CrashReport.UserStrategy(appContext);
        strategy.setUploadProcess(processName == null || processName.equals(packageName));
        // 初始化Bugly
        CrashReport.initCrashReport(appContext, buglyAppID, true, strategy);
    }

    public static ArrayList<PermissionTabBean.DataBean> getPermissionDataBeans() {
        return permissionDataBeans;
    }

    public static void setPermissionDataBeans(ArrayList<PermissionTabBean.DataBean> permissionDataBeans) {
        MainAppHelper.permissionDataBeans = permissionDataBeans;
    }

    /**
     * 获取进程号对应的进程名
     *
     * @param pid 进程号
     * @return 进程名
     */
    private static String getProcessName(int pid) {
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader("/proc/" + pid + "/cmdline"));
            String processName = reader.readLine();
            if (!TextUtils.isEmpty(processName)) {
                processName = processName.trim();
            }
            return processName;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException exception) {
                exception.printStackTrace();
            }
        }
        return null;
    }

    public static void recyclePushServiceAndNotify(){
        String employeeId = LoginBean.EmployeeBean.getEmployeeCode();
        if(!TextUtils.isEmpty(employeeId)){
            JPushInterface.deleteAlias(MainApp.getInstance(),
                                    1);
        }
        JPushInterface.clearAllNotifications(MainApp.getInstance());
        JPushInterface.stopPush(MainApp.getInstance());
//        JPushInterface.onPause(MainApp.getInstance());
    }


}
